package com.x.crm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.crm.DocDesc;
import com.x.crm.http.response.follow.ConditionResponse;
import com.x.crm.model.*;
import com.x.crm.rpc.CrmSysParamRpcService;
import com.x.crm.rpc.CustomerShopRpcService;
import com.x.crm.util.Constant;
import com.x.erp.model.User;
import com.x.erp.rpc.ShopRpcService;
import com.x.erp.rpc.UserRpcService;
import com.x.framework.Base;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sysParam")
public class SysParamController extends BaseController{
	@Reference
	private CrmSysParamRpcService crmSysParamRpcService;
	@Reference
	private CustomerShopRpcService customerShopRpcService;
	@Reference
	private UserRpcService userRpcService;
	@Reference
	private ShopRpcService shopRpcService;


	@DocDesc("质保等级查询")
	@RequestMapping("/warranty-list")
	public ResultModel warrantyList(Warranty warranty) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectWarrantyList(warranty));
	}


	@DocDesc("质保等级数量")
	@RequestMapping("/warrantyNum")
	public ResultModel warrantyNum(Warranty warranty) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectWarrantyNum(warranty));
	}


	@DocDesc("质保等级新增")
	@RequestMapping("/warranty-insert")
	public ResultModel warrantyInsert(Warranty warranty) {
		warranty.setWarrantyCreateTime(new Date());
		crmSysParamRpcService.insertWarranty(warranty);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}

	@DocDesc("质保等级修改")
	@RequestMapping("/warranty-update")
	public ResultModel warrantyUpdate(Warranty warranty) {
		crmSysParamRpcService.updateWarranty(warranty);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	@DocDesc("质保等级删除")
	@RequestMapping("/warranty-delete")
	public ResultModel warrantyDelete(Warranty warranty) {
		crmSysParamRpcService.deleteWarranty(warranty);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}


	@DocDesc("渠道查询")
	@RequestMapping("/channel-list")
	public ResultModel channelList(Channel channel) {
		List<Channel>  res =  crmSysParamRpcService.selectChannelList(channel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",res );
	}


	@DocDesc("渠道查询数量")
	@RequestMapping("/channelNum")
	public ResultModel channelNum(Channel channel) {
		int  res =  crmSysParamRpcService.selectChannelListNum(channel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	@DocDesc("渠道新增")
	@RequestMapping("/channel-insert")
	public ResultModel channelInsert(Channel channel) {
		channel.setChannelCreateTime(new Date());
		crmSysParamRpcService.insertChannel(channel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}


	@DocDesc("渠道修改")
	@RequestMapping("/channel-update")
	public ResultModel channelUpdate(Channel channel ) {
		crmSysParamRpcService.updateChannel(channel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	@DocDesc("渠道删除")
	@RequestMapping("/channel-delete")
	public ResultModel channelDelete(Channel channel) {
		crmSysParamRpcService.deleteChannel(channel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}


	@DocDesc("客户等级查询")
	@RequestMapping("/customerLevel-list")
	public ResultModel cutomerLevelList(CustomerLevel customerLevel) {
		List<CustomerLevel>  res = crmSysParamRpcService.selectCustomerLevelList(customerLevel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	@DocDesc("客户等级查询")
	@RequestMapping("/customerLevelNum")
	public ResultModel cutomerLevelNum(CustomerLevel customerLevel) {
		int  res = crmSysParamRpcService.selectCustomerLevelListNum(customerLevel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	@DocDesc("客户等级新增")
	@RequestMapping("/customerLevel-insert")
	public ResultModel cutomerLevelInsert(CustomerLevel customerLevel) {
		customerLevel.setLevelCreateTime(new Date());
		crmSysParamRpcService.insertCustomerLevel(customerLevel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}

	@DocDesc("客户等级修改")
	@RequestMapping("/customerLevel-update")
	public ResultModel customerLevelUpdate(CustomerLevel customerLevel) {
		crmSysParamRpcService.updateCustomerLevel(customerLevel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	@DocDesc("客户等级删除")
	@RequestMapping("/customerLevel-delete")
	public ResultModel customerLevelDelete(CustomerLevel customerLevel) {
		crmSysParamRpcService.deleteCustomerLevel(customerLevel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}


	@DocDesc("保险公司查询")
	@RequestMapping("/insurance-list")
	public ResultModel insuranceList(Insurance insurance) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectInsuranceList(insurance));
	}

	@DocDesc("保险公司查询")
	@RequestMapping("/insuranceNum")
	public ResultModel insuranceNum(Insurance insurance) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectInsuranceNum(insurance));
	}


	@DocDesc("保险公司新增")
	@RequestMapping("/insurance-insert")
	public ResultModel insuranceInsert(Insurance insurance) {
		insurance.setInsuranceCreateTime(new Date());
		crmSysParamRpcService.insertInsurance(insurance);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}

	@DocDesc("保险公司修改")
	@RequestMapping("/insurance-update")
	public ResultModel insuranceUpdate(Insurance insurance ) {
		crmSysParamRpcService.updateInsurance(insurance);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	@DocDesc("保险公司删除")
	@RequestMapping("/insurance-delete")
	public ResultModel insuranceDelete(Insurance insurance) {
		crmSysParamRpcService.deleteInsurance(insurance);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}


	@DocDesc("按揭公司查询")
	@RequestMapping("/mortgage-list")
	public ResultModel mortgageList(Mortgage mortgage) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectMortgageList(mortgage));
	}

	@DocDesc("按揭公司数量")
	@RequestMapping("/mortgageNum")
	public ResultModel mortgageNum(Mortgage mortgage) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", crmSysParamRpcService.selectMortgageNum(mortgage));
	}


	@DocDesc("按揭公司新增")
	@RequestMapping("/mortgage-insert")
	public ResultModel mortgageInsert(Mortgage mortgage) {
		mortgage.setMortgageCreateTime(new Date());
		crmSysParamRpcService.insertMortgage(mortgage);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！");
	}

	@DocDesc("按揭公司修改")
	@RequestMapping("/mortgage-update")
	public ResultModel mortgageUpdate(Mortgage mortgage) {
		crmSysParamRpcService.updateMortgage(mortgage);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	@DocDesc("按揭公司删除")
	@RequestMapping("/mortgage-delete")
	public ResultModel mortgageDelete(Mortgage mortgage) {
		crmSysParamRpcService.deleteMortgage(mortgage);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功！");
	}


	@RequestMapping(value = "/selectCoopertypeList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCoopertypeList(Coopertype coopertype) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", crmSysParamRpcService.selectCoopertypeList(coopertype));
	}

	@RequestMapping(value = "/selectCoopertypeNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCoopertypeNum(Coopertype coopertype) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", crmSysParamRpcService.selectCoopertypeNum(coopertype));
	}


	@RequestMapping(value = "/insertOrUpdateCoopertype", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertOrUpdateCoopertype(Coopertype coopertype) {
		if (coopertype == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "类型不能为空！");
		}
		if (coopertype.getCoopertypeName() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "名称不能为空！");
		}
		if(this.isNotNull(coopertype.getCoopertypeId())){
			int res = crmSysParamRpcService.updateCoopertype(coopertype);
			if (res == 0){
				return new ResultModel(ResultCode.RESULT_FAIL, "更新失败");
			}
		}else{
			coopertype.setCoopertypeState(Constant.COOPERTYPE_VALID);
			coopertype.setCreateTime(this.getDate(0));
			coopertype.setUpdateTime(this.getDate(0));
			crmSysParamRpcService.insertCoopertype(coopertype);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");
	}

	@RequestMapping(value = "/freezeOprCoopertype" , method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel freezeCooperation(Coopertype coopertype) {
		if (coopertype.getCoopertypeId() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "id不能为空！");
		}
		if (coopertype.getCoopertypeState() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "状态不能为空！");
		}
		crmSysParamRpcService.updateCoopertype(coopertype);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");
	}




	@RequestMapping(value = "/selectIntentionLevelList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectIntentionLevelList(IntentionLevel intentionLevel) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", crmSysParamRpcService.selectIntentionLevelList(intentionLevel));
	}

	@RequestMapping(value = "/selectIntentionLevelNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectIntentionLevelNum(IntentionLevel intentionLevel) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", crmSysParamRpcService.selectIntentionLevelNum(intentionLevel));
	}


	@RequestMapping(value = "/insertOrUpdateIntentionLevel", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertOrUpdateIntentionLevel(IntentionLevel intentionLevel) {
		if (intentionLevel == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "类型不能为空！");
		}
		if (intentionLevel.getIntentionLevelTxt() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "名称不能为空！");
		}
		if(this.isNotNull(intentionLevel.getIntentionLevelId())){
			int res = crmSysParamRpcService.updateIntentionLevel(intentionLevel);
			if (res == 0){
				return new ResultModel(ResultCode.RESULT_FAIL, "更新失败");
			}
		}else{
			intentionLevel.setIntentionLevelState(Constant.INTENTION_LEVEL_VALID);
			intentionLevel.setIntentionLevelCreateTime(this.getDate(0));
			intentionLevel.setIntentionLevelUpdateTime(this.getDate(0));
			crmSysParamRpcService.insertIntentionLevel(intentionLevel);
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");

	}


	@RequestMapping(value = "/freezeOprIntentionLevel" , method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel freezeIntentionLevel(IntentionLevel intentionLevel) {
		if (intentionLevel.getIntentionLevelId() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "id不能为空！");
		}
		if (intentionLevel.getIntentionLevelState() == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "状态不能为空！");
		}
		crmSysParamRpcService.updateIntentionLevel(intentionLevel);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "操作成功");
	}





	@DocDesc("参数配置接口请求")
	@RequestMapping("/paramInfoList")
	
	public ResultModel paramInfoList(HttpSession session, HttpServletResponse response1) {
		response1.setHeader("Access-Control-Allow-Origin", "*");
		ConditionResponse response = new ConditionResponse();
		User user = (User) session.getAttribute(SessionKey.loginUser);
		String marketId = user.getMarketId();
		List<Channel> channelList = crmSysParamRpcService.selectChannelListAll(new Channel(marketId, null));
		List<CustomerLevel> customerLevelList = crmSysParamRpcService.selectCustomerLevel(new CustomerLevel(marketId, null));
		List<Insurance> insuranceList = crmSysParamRpcService.selectInsuranceList(new Insurance(marketId, null));
		List<Mortgage> mortgageList = crmSysParamRpcService.selectMortgageList(new Mortgage(marketId, null));
		List<Warranty> warrantyList = crmSysParamRpcService.selectWarrantyList(new Warranty(marketId, null));
		response.setChannelList(channelList);
		response.setCustomerLeveList(customerLevelList);
		response.setInsuranceList(insuranceList);
		response.setMortgageList(mortgageList);
		response.setWarrantyList(warrantyList);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", response);

	}


}
